package com.ciwong.epaper.modules.epaper.util;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

/**
 * Created by lzr on 2016/9/23.
 */
public class HorizontalScrollClick extends HorizontalScrollView {

    private ScrollEvent scrollEvent;

    public HorizontalScrollClick(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public HorizontalScrollClick(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalScrollClick(Context context) {
        super(context);
    }

    public void setScrollEvent(ScrollEvent scrollEvent) {
        this.scrollEvent = this.scrollEvent;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        int  maxScrollX = getChildAt(0).getMeasuredWidth()-getMeasuredWidth();
        //滑到最左
        if (getScrollX() <= 10 ) {
            if(scrollEvent !=null){
                scrollEvent.toLeft();
            }
        }else if (getScrollX() == maxScrollX) {  //滑到最右
            if(scrollEvent !=null){
                scrollEvent.toRight();
            }
        }else {  //滑到中间
            if(scrollEvent !=null){
                scrollEvent.other();
            }
        }
    }

    public interface ScrollEvent {
       void toLeft();
         void toRight();
       void other();
    }
}
